package com.swordget.entity.vo;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @Author: David
 * @Date: Created in 17:10 2019/12/18
 * @Description: 统一的响应类，定义统一响应信息
 * 由于需求不大，所以没有统一定义所有错误类型直接采用参数的方式
 * 000000 成功 000001 失败
 * @Modified By:
 * @Version:
 */
public class SimpleResponseVo<T> {

    /**
     * SimpleResponseVo log
     */
    private final static Logger LOG = LoggerFactory.getLogger(SimpleResponseVo.class);

    /**
     * 失败原因
     */
    private String msg;

    /**
     * 失败码
     */
    private String code;

    /**
     * 数据
     */
    private T data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 成功响应
     * @return
     */
    public static SimpleResponseVo success() {
        SimpleResponseVo responseVo = new SimpleResponseVo();
        responseVo.setCode("000000");
        responseVo.setMsg("处理成功");
        return responseVo;
    }

    /**
     * 成功响应
     * @param data 数据
     * @return
     */
    public static SimpleResponseVo success(String data) {
        SimpleResponseVo<String> responseVo = new SimpleResponseVo();
        responseVo.setCode("000000");
        responseVo.setMsg("处理成功");
        responseVo.setData(data);
        return responseVo;
    }

    /**
     * 成功响应
     * @param json 数据
     * @return
     */
    public static SimpleResponseVo success(Object json) {
        SimpleResponseVo responseVo = new SimpleResponseVo();
        responseVo.setCode("000000");
        responseVo.setMsg("处理成功");
        responseVo.setData(json);
        return responseVo;
    }


    /**
     * 失败响应
     * @param failMsg  失败原因
     */
    public static SimpleResponseVo fail(String failCode, String failMsg) {
        SimpleResponseVo responseVo = new SimpleResponseVo();
        //没有指定失败码，给默认000001
        if (StringUtils.isBlank(failCode)) {
            failCode = "000001";
        }
        responseVo.setCode(failCode);
        responseVo.setMsg(failMsg);
        return responseVo;
    }

    public static SimpleResponseVo fail(String failMsg) {
        return fail("000010", failMsg);
    }


    /**
     * 向响应流中写入响应数据,隐藏具体实现
     * @param response 响应
     * @param responseText 需要写入的响应数据
     */
    private static void write(HttpServletResponse response, String responseText) {
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter writer = response.getWriter()) {
            System.out.println(responseText);
            writer.print(responseText);
            writer.flush();
        } catch (IOException e) {
            LOG.error("程序执行过程中出现异常", e);
        }
    }

    /**
     * 向响应中写入数据
     * @param response 响应
     * @param responseVo 要写入的vo类，后期可以对这个方法扩展此处扩展为泛型，以支持更多vo
     */
    public static void response(HttpServletResponse response, SimpleResponseVo responseVo) {
        //格式化为json数据
        /*JSONObject jsonObject = JSONObject.fromObject(responseVo);*/
        //响应客户端
        /*write(response, jsonObject.toString());*/
    }

    public static void response(HttpServletResponse response, String content) {
        write(response, content);
    }

    /**
     * 要校验路径，不然可以下载服务器所有文件
     * @param path 要下载文件的本地路径
     * @param response 响应
     */
    public static void responseFile(String path, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");
        File file = new File(path);
        response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(file);
            outputStream = response.getOutputStream();
            byte[] bytes = new byte[1024];
            int readByte = 0;
            while ((readByte = inputStream.read(bytes)) > 0) {
                outputStream.write(bytes, 0, readByte);
            }
        } catch (FileNotFoundException e) {
            LOG.error("程序执行过程中出现异常", e);
        } catch (IOException e) {
            LOG.error("程序执行过程中出现异常", e);
        } finally {
            if (null != outputStream) {
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    LOG.error("程序执行过程中出现异常", e);
                }
            }
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    LOG.error("程序执行过程中出现异常", e);
                }
            }
        }
    }

}
